home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / UserCode / smartSource.tcl < prev    next >
Text File  |  1994-10-12  |  3KB  |  77 lines

  1. #==============================================================================
  2. # smart-source.tcl
  3. #
  4. # Copyright 1994, Robert Browning (osiris@cs.utexas.edu): This code is made
  5. # freely available to anyone who can find a use for it. Consider it part of my
  6. # thanks to all those who have contributed to the freeware and shareware base.
  7. # (Especially Peter Keheler, without which this code would be pretty useless).
  8. #
  9. #==============================================================================
  10. #
  11. # This file implements a replacement source command. It is intended to help
  12. # make the process of augmenting Alpha's code base with your own changes less
  13. # awkward, especially in the face of program updates. It also makes it so
  14. # that you no longer have to source files containing procedures that you want
  15. # to override in your UserStartup file.
  16. #
  17. # Basically, you designate a directory (right now it has to be a directory
  18. # named TclExtensions in the $HOME directory) to contain your files. Then
  19. # anytime Alpha is instructed to source a file named filename.tcl, it will
  20. # first look in the directory you have designated, and if there is a file of
  21. # the identical name there (i.e. filename.tcl) it will source that file
  22. # instead of the original one specified. If there are any files named
  23. # filename**.tcl it will source those in the order returned by glob after
  24. # either the original filename.tcl or the replacement has been sourced.
  25. #
  26. # For example, if your TclExtensions directory contains files named
  27. # latex*1.tcl and latex*2.tcl, then whenever you try to open a latex file
  28. # Alpha will first source the standard latex.tcl file, then latex*1.tcl, then
  29. # latex*2.tcl. If there was also a file named latex.tcl in the TclExtensions
  30. # directory then that file would be sourced in place of the standard
  31. # latex.tcl, then latex*1.tcl, then latex*2.tcl.
  32. #
  33. # You may just want to use filename*.tcl for a single extension file.
  34. #
  35. #==============================================================================
  36. #
  37. # To Do:
  38. #
  39. # Add the ability to dynamically locate the TclExtensions directory.
  40. #
  41. # (Feel free to make suggestions or improve this code.)
  42. #
  43. #==============================================================================
  44.  
  45. set tclExtensionsDir "$HOME:TclExtensions"
  46.  
  47. if {[file exists "$tclExtensionsDir"]} {
  48.     if {![string length [info commands oldSource]]} {
  49.         rename source oldSource
  50.     }
  51.  
  52.     proc source {filename} {
  53.         global tclExtensionsDir
  54.  
  55.         set justName [file tail "$filename"]
  56.         set overrideFile "${tclExtensionsDir}:${justName}"
  57.  
  58.         if {[file exists $overrideFile]} {
  59.             set returnVal [uplevel 1 oldSource "$overrideFile"]
  60.         } else {
  61.             set returnVal [uplevel 1 oldSource "$filename"]
  62.         }
  63.  
  64.         set baseName [string range ${justName} 0 \
  65.             [expr   [string length ${justName}] - \
  66.                     [string length [file extension ${justName}]] - 1]]
  67.  
  68.         set extensionFiles \
  69.             [glob -nocomplain "${tclExtensionsDir}:${baseName}**.tcl"]
  70.  
  71.         foreach extensionFile $extensionFiles {
  72.             set returnVal [uplevel 1 oldSource "$extensionFile"]
  73.         }
  74.         return "$returnVal"
  75.     }
  76. }
  77.